package com.prs.crm.action.oa;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.oa.Apply;
import com.prs.crm.domain.oa.MyFile;
import com.prs.crm.domain.oa.MyFileFolder;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.oa.MyFileFolderService;
import com.prs.crm.service.oa.MyFileService;

public class MyFileAction extends BaseAction{
	private static final long serialVersionUID = -6136720957125121468L;
	private MyFile myFile;
	private MyFileService myFileService;
	private MyFileFolderService myFileFolderService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private String appIds;
	private File upload;
	private String uploadFileName;
	private MyFileFolder myFileFolder;
	
	@Action("listMyFile")
	public String listMyFile(){
		return SUCCESS;
	}
	
	@Action("editMyFile")
	public String editMyFile(){
		return SUCCESS;
	}
	
	@Action("viewMyFile")
	public String viewMyFile(){
		return SUCCESS;
	}
	
	@Action(value = "removeMyFile", results = {
			@Result(name = "success", type = "redirect", location = "listMyFile"),
			@Result(name = "error", type = "chain", location = "listMyFile") })
	public String removeMyFile(){
		if(ids == null){
			this.addActionError("请选择要删除的文件");
			return ERROR;
		}
		this.getMyFileService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveMyFile", results = {
			@Result(name = "success", type = "redirect", location = "editMyFile"),
			@Result(name = "error", type = "chain", location = "editMyFile") })
	public String saveMyFile(){
		if (myFile.getTitle() == null || myFile.getTitle().trim().equals("")) {
			this.addActionError("标题不能为空");
			return ERROR;
		}
		if (upload == null) {
			this.addActionError("文件不能为空");
			return ERROR;
		}
		if (myFile.getFolder() == null) {
			this.addActionError("所在文件夹不能为空");
			return ERROR;
		}
		this.setPrompt("文件保存成功");
		this.getMyFileService().saveFile(myFile, upload, uploadFileName);
		return SUCCESS;
	}
	
	@Action("listMyFileByFolder")
	public String listMyFileByFolder(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyFile.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (appIds != null) {
			if (!"all".equals(appIds)) {
				Collection<Integer> personIds = new ArrayList<Integer>();
				String levels = "";
				for (String val : appIds.split(",")) {
					if (val.indexOf("dept") == -1) {
						if (ToolKits.isInteger(val)) {
							personIds.add(Integer.parseInt(val));
						} else {
							levels = val;
						}
					}
				}
//				if (personIds.size() > 0) {
//					criteria.createAlias("recorder", "ri");
//					criteria.add(Restrictions.in("ri.id", personIds));
//				}
//				if (!"".equals(levels.trim())) {
//					criteria.add(Restrictions.eq("level", ReportUtils
//							.URLDecode(levels.trim())));
//				}
			}
		}
		this.setPagination(this.getMyFileService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	public MyFile getMyFile() {
		return myFile;
	}
	public void setMyFile(MyFile myFile) {
		this.myFile = myFile;
	}
	public MyFileService getMyFileService() {
		return myFileService;
	}
	public void setMyFileService(MyFileService myFileService) {
		this.myFileService = myFileService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public String getAppIds() {
		return appIds;
	}

	public void setAppIds(String appIds) {
		this.appIds = appIds;
	}

	public MyFileFolderService getMyFileFolderService() {
		return myFileFolderService;
	}

	public void setMyFileFolderService(MyFileFolderService myFileFolderService) {
		this.myFileFolderService = myFileFolderService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public MyFileFolder getMyFileFolder() {
		return myFileFolder;
	}

	public void setMyFileFolder(MyFileFolder myFileFolder) {
		this.myFileFolder = myFileFolder;
	}
	
}
